const {Article} = require('../../model/article');

const formidable = require('formidable');

const path  = require('path');

module.exports= async (req,res)=>{


	const{title,publishDate,cover,content}= req.body;
	

	const form = new formidable.IncomingForm();
	//配置上传文件的存放位置
	form.uploadDir = path.join(__dirname,'../','../','public','uploads');

	//保留上传文件的后缀
	form.keepExtensions = true;

	
	form.parse(req, async (err,fields,files)=>{
    
     const id = req.query.id;

	// err 错误对象：如果表单解析失败，err里面存储错误信息，如果表单解析成功，err将会是空
	// fields 对象类型 保存普通表单数据
	// files 对象类型保存了和上传文件相关的数据

   await Article.updateOne({_id:id},{
	title:fields.title,
	author:fields.author,
	publishDate:fields.publishDate,
	cover:files.cover.path.split('public')[1],
	content:fields.content
});

res.redirect('/admin/article');

});

	
}